/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.util;

import com.yanlight.Constants;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

/**
 * @author haolw
 * @time 2017/08/28
 */
public abstract class PayQRcodeUtil {
    private static final int PIXEL = QRCodeUtil.PIXEL;
    private static final int DEFAULT_RATE = QRCodeUtil.DEFAULT_RATE;


    public static void createDownloadQRImage(String content, Integer rate, String name, String type, HttpServletResponse response) {

        if (rate == null) {
            rate = DEFAULT_RATE;
        }

        try {

            BufferedImage background = null;
            URL bgUrl = null;
            int startX = 0, startY = 0, size = 0;

            if (type.equals("9_14")) {
                bgUrl = new URL("https://yanlight-trade.oss-cn-zhangjiakou.aliyuncs.com/images/bg/qr_code_container_rect.jpg");
                startX = 256;
                startY = 390;
                size = 487;

            } else if (type.equals("9_9")) {
                bgUrl = new URL("https://yanlight-trade.oss-cn-zhangjiakou.aliyuncs.com/images/bg/qr_code_container_square.jpg");
                startX = 279;
                startY = 273;
                size = 441;
            }
            
            if (bgUrl != null) {
                background = ImageIO.read(bgUrl);
                BufferedImage QRImg = QRCodeUtil.creatQRImage(rate, content);
                Graphics2D graphics = background.createGraphics();
                graphics.drawImage(QRImg, startX, startY, size, size, null);
                graphics.dispose();

                outputImage(name, background, rate, response);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return;
    }

    /**
     * 创建二维码图片 默认倍率：6，QR.width:258,height:258
     *
     * @param rate
     * @param content
     * @throws Exception
     */
    public static void creatQRImage(String content, Integer rate, HttpServletResponse response) {
        try {

            if (rate == null) {
                rate = DEFAULT_RATE;
            }
            BufferedImage QRImg = QRCodeUtil.creatQRImage(rate, content);


            outputImage(null, QRImg, rate, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return;
    }


    private static void outputImage(String fileName, BufferedImage image, Integer rate, HttpServletResponse response) {
        try {
            int length = PIXEL * rate;
            if (StringUtils.isBlank(fileName)) {
                fileName = "qr_code_" + length + ".jpg";
            } else {
                fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1") + ".jpg";
            }

            response.setHeader("content-disposition", "attachment;filename=" + fileName);
            OutputStream outStream = response.getOutputStream();

            ImageIO.write(image, Constants.QR_IMG_FORMAT, outStream);
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
